<?php

namespace app\api\controller;

use app\common\controller\Api;

class Salary extends Api
{
    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 工资单列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function list()
    {
        $userInfo = $this->auth->getUserinfo();
        $teacherInfo = \app\admin\model\Teacher::get([
            'mobile' => $userInfo['mobile']
        ]);

        $time = date('Y');
        if (input('time')) {
            $time = input('time');
        }

        $list = \app\admin\model\Salary::where('salary_month', 'like', $time . '%')
            ->where(['user_id' => $teacherInfo->id])
            ->order('salary_month', 'asc')
            ->field('id, salary_month, send_time, issued_amount')
            ->select();

        $total = 0;
        if (!empty($list)) {
            foreach ($list as $item => $value) {
                $list[$item]->salary_month = explode('年', $value->salary_month)[1];
                $total += $value->issued_amount;
            }
        }

        $this->success('成功', [
            'userinfo' => [
                'avatar' => $userInfo['avatar'],
                'name' => $teacherInfo->name,
                'subject' => $teacherInfo->subject,
                'teaching_subject' => $teacherInfo->teaching_subject,
                'department' => $teacherInfo->department,
                'total' => $total
            ],
            'salary' => $list
        ]);
    }

    /**
     * 工资详情
     * @throws \think\exception\DbException
     */
    public function info()
    {
        $id = input('id') ?? null;
        if (!$id) {
            $this->error(__('Invalid parameters'));
        }
        $salaryInfo = \app\admin\model\Salary::get($id);
        $userInfo = $this->auth->getUserinfo();
        $teacherInfo = \app\admin\model\Teacher::get([
            'mobile' => $userInfo['mobile']
        ]);

        $this->success('成功', [
            'info' => $salaryInfo,
            'userInfo' => [
                'avatar' => $userInfo['avatar'],
                'name' => $teacherInfo->name,
                'subject' => $teacherInfo->subject,
                'teaching_subject' => $teacherInfo->teaching_subject,
                'department' => $teacherInfo->department,
            ]
        ]);
    }
}